package tech.summerly.weather.data.local

import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.experimental.runBlocking
import java.lang.reflect.Type

/**
 * Created by summer on 18-3-16
 */
class SimplePersistence(
        override val name: String
) : Persistence() {


    operator fun set(key: String, any: Any?) {
        any ?: return
        putObject(key, any)
    }

    inline operator fun <reified T> get(key: String): T? {
        val type = object : TypeToken<T>() {}.type
        return get<T>(key, type)
    }

    operator fun <T> get(key: String, type: Type): T? = runBlocking {
        return@runBlocking getObject<T>(key, type).await()
    }
}